//
//  ShopAdornViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/10.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ShopAdornViewController.h"
#import "ShopGoodsDetailVC.h"

#import "SearchViewController.h"
#import "ShopSetViewController.h"
#import "FPBottomButtonView.h"
#import "ShopAdornGoodsTableViewCell.h"
#import "ShopTotalCommentCell.h"
#import "CreateSpaceModel.h"
#import "ShopGoodsTableViewCell.h"
#import "AdornAddGoodsCell.h"
#import <MJRefresh.h>

@interface ShopAdornViewController ()<UITableViewDelegate, UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) BaseTableView         *tableView;
@property (nonatomic, strong) FPBottomButtonView    *buttonView;
@property (nonatomic, strong) ShopInfo              *model;
@property (nonatomic, strong) UIButton              *selectBtn;
@property (nonatomic, strong) NSMutableString       *type;
@property (nonatomic, strong) UIView                *operateView;
@property (nonatomic, strong) NSMutableArray        *createSpaceData;
@property (nonatomic, strong) NSMutableArray        *dataArray;
@property (nonatomic, strong) NSMutableArray        *pageArray;
@property (nonatomic, strong) NSMutableArray        *currentPageArray;
@property (nonatomic, assign) NSInteger             saleStatu;
@property (nonatomic, assign) NSInteger             CPcurrentPage;
@property (nonatomic, assign) NSInteger             CPtotalPage;
@property (nonatomic, strong) UIImageView * headImage;
@property (nonatomic, strong) UILabel     * nameLabel;
@property (nonatomic, strong) UILabel     * detailLabel;

@property (nonatomic, strong) UIButton      * addGoodsView;
@property (nonatomic, strong) BaseTableView *goodsTableView;
@property (nonatomic, assign) BOOL           isHomeSet;
@property (nonatomic, assign) NSInteger      selectIndex;
@property (nonatomic, strong) NSMutableArray *addGoodsArray;
@property (nonatomic, assign) NSInteger      page;
@property (nonatomic, assign) NSInteger      currentPage;
@property (nonatomic, strong) UIImagePickerController *pickerController;
@property (nonatomic, strong) FPCustomButton *addPhotoBtn;
@property (nonatomic, strong) UIImage        *addImage;

@end

@implementation ShopAdornViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self setNavBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _createSpaceData = [NSMutableArray array];
    _model = [[ShopInfo alloc] init];
    self.dataArray = [NSMutableArray array];
    self.pageArray = [NSMutableArray array];
    self.addGoodsArray = [NSMutableArray array];
    self.saleStatu = 1;
    self.CPcurrentPage = 1;
    self.selectIndex = -1;
    self.currentPage = 1;
    self.currentPageArray = [NSMutableArray array];\
    
    for (int i = 0; i < 4; i++) {
        [self.dataArray addObject:[NSMutableArray array]];
        [self.currentPageArray addObject:@"1"];
        [self.pageArray addObject:@"1"];
    }
    _type = [[NSMutableString alloc] initWithString:@"0"];
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT - ScreenX375(49))];
    if (_isMine) {
        self.tableView.frame = CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT);
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(101);
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    if (!_isMine) {
        _buttonView = [FPBottomButtonView initBottomButtonViewWithTitle:@"雇佣他"];
        _buttonView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(49), SCREEN_WIDTH, ScreenX375(49));
        _buttonView.threeTextButton.hidden = YES;
        _buttonView.payAttentionClick = ^{
            [HttpManager showWait];
            [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/collect_shop",REQUESTHEADER] andParameter:@{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":self.shop_id} success:^(id successResponse) {
                NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                [HttpManager hideWait];
                if ([status isEqualToString:@"1"]) {
                    [HttpManager showNoteMsg:@"操作成功"];
                }else{
                    [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
                }
            } andFailure:^(id failureResponse) {
                [HttpManager showFail];
            }];
        };
        [self.view addSubview:_buttonView];
    }
    self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    [self getData];
    [self getCreateSpaceDate];
    [self getGoodsData];
    [kNotificationCenter addObserver:self selector:@selector(getData) name:@"ReloadShop" object:nil];
}

- (void)dealloc {
    [kNotificationCenter removeObserver:self name:@"ReloadShop" object:nil];
}

- (void)setNavBar {
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 44)];
    [backBtn setImage:[UIImage imageNamed:@"返回白色"] forState:UIControlStateNormal];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 52, 30)];
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 22, 22)];
    leftImg.image = [UIImage imageNamed:@"搜索"];
    [leftView addSubview:leftImg];
    
    UITextField *searchField = [[UITextField alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH - ScreenX375(54), 30)];
    searchField.placeholder = @"找专业的人才做专业的事情";
    searchField.layer.cornerRadius = ScreenX375(15);
    searchField.clipsToBounds = YES;
    searchField.backgroundColor = [UIColor whiteColor];
    searchField.leftViewMode = UITextFieldViewModeAlways;
    searchField.leftView = leftView;
    searchField.font = Kfont(13);
    self.navigationItem.titleView = searchField;
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - ScreenX375(54), 30)];
    searchButton.backgroundColor = [UIColor clearColor];
    [searchButton addTarget:self action:@selector(turnToSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem.titleView addSubview:searchButton];
    
    if (!_isMine) {
        UIButton *shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 44)];
        [shareBtn setImage:[UIImage imageNamed:@"分享白色"] forState:UIControlStateNormal];
        shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    }
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData {
    NSString *url = [NSString stringWithFormat:@"%@app/shop_info",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken]};
    if (!_isMine) {
        url = [NSString stringWithFormat:@"%@app/other_shop_info",REQUESTHEADER];
        dict = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":_shop_id,@"longitude":kNSUDefaultReadKey(@"longitude"),@"latitude":kNSUDefaultReadKey(@"latitude")};
    }
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error;
            self.model = [[ShopInfo alloc] initWithDictionary:successResponse[@"data"] error:&error];
            [self setHead];
            [self.tableView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)turnToSearch {
    SearchViewController *vc = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)headerRefresh {
    if ([_type isEqualToString:@"1"]) {
        [self.currentPageArray setObject:@"1" atIndexedSubscript:_saleStatu];
        [self getGoodsData];
    }else if([_type isEqualToString:@"2"]){
        self.CPcurrentPage = 1;
        [self getCreateSpaceDate];
        self.tableView.mj_footer.hidden = NO;
    }
}


- (void)footerRefresh {
    if ([_type isEqualToString:@"1"]) {
        if ([self.currentPageArray[_saleStatu] integerValue] < [self.pageArray[_saleStatu] integerValue]) {
            NSInteger currentPage = [self.currentPageArray[_saleStatu] integerValue];
            currentPage++;
            [self.currentPageArray setObject:[NSString stringWithFormat:@"%ld",(long)currentPage] atIndexedSubscript:_saleStatu];
            [self getGoodsData];
            // 结束刷新
            [self.tableView.mj_footer endRefreshing];
        }else{
            [HttpManager showNoteMsg:@"已经到底啦"];
            // 结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
    }else if([_type isEqualToString:@"2"]){
        if (_CPcurrentPage < _CPtotalPage) {
            
            _CPcurrentPage++;
            [self getCreateSpaceDate];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [HttpManager showNoteMsg:@"已经到底啦"];
            [self.tableView.mj_footer endRefreshing];
            self.tableView.mj_footer.hidden = YES;
        }
    }
    
}

- (void)setHead {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(192) + STATUS_BAR_HEIGHT)];
    self.tableView.tableHeaderView = headView;
    
    UIImageView *headBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(192) + STATUS_BAR_HEIGHT)];
    headBg.image = [UIImage imageNamed:@"头部背景"];
    [headView addSubview:headBg];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, 44, 44)];
    [backBtn setImage:[UIImage imageNamed:@"返回白色"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:backBtn];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 52, 30)];
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 22, 22)];
    leftImg.image = [UIImage imageNamed:@"搜索"];
    [leftView addSubview:leftImg];
    
    UITextField *searchField = [[UITextField alloc] initWithFrame:CGRectMake(44, 7 + STATUS_BAR_HEIGHT, ScreenX375(288), 30)];
    searchField.placeholder = @"找专业的人才做专业的事情";
    searchField.layer.cornerRadius = ScreenX375(15);
    searchField.clipsToBounds = YES;
    searchField.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
    searchField.leftViewMode = UITextFieldViewModeAlways;
    searchField.leftView = leftView;
    searchField.font = Kfont(13);
    [headView addSubview:searchField];
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(288), 30)];
    searchButton.backgroundColor = [UIColor clearColor];
    [searchButton addTarget:self action:@selector(turnToSearch) forControlEvents:UIControlEventTouchUpInside];
    [searchField addSubview:searchButton];
    
    if (!_isMine) {
        UIButton *shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 44, STATUS_BAR_HEIGHT, 44, 44)];
        [shareBtn setImage:[UIImage imageNamed:@"分享白色"] forState:UIControlStateNormal];
        [headView addSubview:shareBtn];
    }
    
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(54) + STATUS_BAR_HEIGHT, ScreenX375(72), ScreenX375(72))];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:_model.logo] placeholderImage:nil options:SDWebImageRetryFailed];
    self.headImage.layer.cornerRadius = ScreenX375(5);
    self.headImage.clipsToBounds = YES;
    [headView addSubview:self.headImage];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(95), ScreenX375(52) + STATUS_BAR_HEIGHT, ScreenX375(200), ScreenX375(17))];
    self.nameLabel.text = _model.name;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = Kfont(14);
    [headView addSubview:self.nameLabel];
    
    self.detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(95), ScreenX375(73) + STATUS_BAR_HEIGHT, ScreenX375(200), ScreenX375(17))];
    self.detailLabel.text = _model.intro;
    self.detailLabel.textColor = [UIColor whiteColor];
    self.detailLabel.font = Kfont(12);
    [headView addSubview:self.detailLabel];
    
    CGFloat width = ScreenX375(95);
    NSArray *arr = _model.labels;
    for (int i = 0; i < arr.count; i++) {
        CGFloat textWidth = kGetStringWidth(arr[i][@"name"], CGSizeMake(MAXFLOAT, ScreenX375(17)), Kfont(10)) + ScreenX375(12);
        UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(width, ScreenX375(97) + STATUS_BAR_HEIGHT, textWidth, ScreenX375(17))];
        tagLabel.text = arr[i][@"name"];
        tagLabel.textColor = [UIColor whiteColor];
        tagLabel.backgroundColor = ThemeColor;
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.layer.cornerRadius = ScreenX375(7.5);
        tagLabel.clipsToBounds = YES;
        tagLabel.font = Kfont(10);
        [headView addSubview:tagLabel];
        
        width = width + ScreenX375(5) + textWidth;
    }
    
    UIView *numView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(141) + STATUS_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(51))];
    numView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [headView addSubview:numView];
    
    NSArray *noteArr = @[@"近三个月收入",@"成交",@"好评"];
    NSArray *numArr = @[@"0",@"0笔",@"100%"];
    CGFloat classWidth = SCREEN_WIDTH / 3;
    for (int i = 0; i < noteArr.count; i++) {
        UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * classWidth, ScreenX375(28), classWidth, ScreenX375(14))];
        noteLabel.text = noteArr[i];
        noteLabel.textColor = [UIColor whiteColor];
        noteLabel.textAlignment = NSTextAlignmentCenter;
        noteLabel.font = Kfont(12);
        [numView addSubview:noteLabel];
        
        UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * classWidth, ScreenX375(7), classWidth, ScreenX375(16))];
        numLabel.text = numArr[i];
        numLabel.textColor = [UIColor whiteColor];
        numLabel.textAlignment = NSTextAlignmentCenter;
        numLabel.font = Kfont(14);
        [numView addSubview:numLabel];
        
        if (i != 0) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i * classWidth, ScreenX375(6), 1, ScreenX375(39))];
            line.backgroundColor = [UIColor whiteColor];
            [numView addSubview:line];
        }
    }
}

#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _goodsTableView) {
        return _addGoodsArray.count;
    }
    if([self.type isEqualToString:@"2"]){
        return _createSpaceData.count + 1;
    }else if ([self.type isEqualToString:@"0"]){
        return _model.pics.count + 1;
    }else if ([self.type isEqualToString:@"3"]){
        return 6;
    }
    return [self.dataArray[_saleStatu] count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _goodsTableView) {
        AdornAddGoodsCell *cell = [AdornAddGoodsCell cellWithTableView:tableView andIndexPath:indexPath];
        if (indexPath.row == _selectIndex) {
            [cell setStatus:YES andModel:_addGoodsArray[indexPath.row]];
        }else {
            [cell setStatus:NO andModel:_addGoodsArray[indexPath.row]];
        }
        return cell;
    }
    if ([_type isEqualToString:@"3"]) {
        if (indexPath.row == 0) {
            static NSString *cellId = @"commentCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"雇主评价"];
            [cell addSubview:headView];
            
            UIImageView *allImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(30), ScreenX375(72), ScreenX375(91), ScreenX375(48))];
            allImg.image = ImageWithName(@"评论条");
            [cell addSubview:allImg];
            
            UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(50), ScreenX375(90), ScreenX375(50), ScreenX375(35))];
            if (_model) {
                allLabel.text = [NSString stringWithFormat:@"%@\n综合得分",_model.total_star];
            }
            allLabel.textAlignment = NSTextAlignmentCenter;
            allLabel.font = Kfont(12);
            allLabel.numberOfLines = 0;
            [cell addSubview:allLabel];
            
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(151), ScreenX375(64), 1, ScreenX375(64))];
            line.backgroundColor = LineColor;
            [cell addSubview:line];
            
            NSArray *noteArr = @[@"完成质量",@"工作速度",@"服务态度"];
            NSArray *valueArr = @[@"5.0",@"4.8",@"4.7"];
            if (_model) {
                valueArr = @[_model.quality_star,_model.speed_star,_model.server_star];
            }
            for (int i = 0; i < noteArr.count; i++) {
                UIImageView *valueImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(179) + i * ScreenX375(65), ScreenX375(65), ScreenX375(44), ScreenX375(44))];
                valueImg.image = ImageWithName(@"评论条5分");
                [cell addSubview:valueImg];
                
                UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(190) + i * ScreenX375(65), ScreenX375(80), ScreenX375(24), ScreenX375(14))];
                valueLabel.text = valueArr[i];
                valueLabel.textAlignment = NSTextAlignmentCenter;
                valueLabel.font = Kfont(12);
                [cell addSubview:valueLabel];
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(175) + i * ScreenX375(65), ScreenX375(114), ScreenX375(52), ScreenX375(14))];
                noteLabel.text = noteArr[i];
                noteLabel.textAlignment = NSTextAlignmentCenter;
                noteLabel.font = Kfont(12);
                [cell addSubview:noteLabel];
            }
            
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(143), SCREEN_WIDTH, ScreenX375(10))];
            bottomView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            [cell addSubview:bottomView];
            return cell;
        }else if (indexPath.row == 1) {
            static NSString *cellId = @"commentDetialCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"雇主印象"];
            [cell addSubview:headView];
            
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(312 - 264), SCREEN_WIDTH, ScreenX375(10))];
            bottomView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            [cell addSubview:bottomView];
            return cell;
        }else if (indexPath.row == 2) {
            static NSString *cellId = @"infoCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"企业简介"];
            [cell addSubview:headView];
            
            UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(64), SCREEN_WIDTH - ScreenX375(26), ScreenX375(52))];
            if (_model) {
                detailLabel.text = _model.intro;
            }
            detailLabel.textColor = REXADECIMALCOLOR(@"#999999");
            detailLabel.numberOfLines = 0;
            detailLabel.font = Kfont(14);
            [cell addSubview:detailLabel];
            
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(130), SCREEN_WIDTH, ScreenX375(10))];
            bottomView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            [cell addSubview:bottomView];
            return cell;
        }else if (indexPath.row == 3) {
            static NSString *cellId = @"addressCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"企业位置"];
            [cell addSubview:headView];
            
            UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(62), ScreenX375(250), ScreenX375(16))];
            if (_model) {
                addressLabel.text = _model.address;
            }
            addressLabel.font = Kfont(14);
            addressLabel.textColor = FONTCOLOR_BLACK;
            [cell addSubview:addressLabel];
            
            UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(84), ScreenX375(200), ScreenX375(16))];
            if (_model) {
                distanceLabel.text = [NSString stringWithFormat:@"距离%@米",_model.distance];
            }
            distanceLabel.font = Kfont(14);
            distanceLabel.textColor = FONTCOLOR_LIGHTGRAY;
            [cell addSubview:distanceLabel];
            
            UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(34), ScreenX375(59), ScreenX375(21), ScreenX375(22))];
            icon.image = ImageWithName(@"地址2");
            [cell addSubview:icon];
            
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(114), SCREEN_WIDTH, ScreenX375(10))];
            bottomView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            [cell addSubview:bottomView];
            return cell;
        }else if (indexPath.row == 4) {
            static NSString *cellId = @"imageCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"营业执照"];
            [cell addSubview:headView];
            
            UIImageView *firstImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(69), ScreenX375(140), ScreenX375(106))];
            firstImg.layer.cornerRadius = ScreenX375(5);
            firstImg.clipsToBounds = YES;
            firstImg.layer.borderColor = FONTCOLOR_LIGHTGRAY.CGColor;
            firstImg.layer.borderWidth = 1;
            if (_model) {
                if (_model.business.count > 0) {
                    [firstImg sd_setImageWithURL:[NSURL URLWithString:_model.business[0][@"qualification_pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
                }
            }
            [cell addSubview:firstImg];
            
            UIImageView *secondImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(163), ScreenX375(69), ScreenX375(140), ScreenX375(106))];
            secondImg.layer.cornerRadius = ScreenX375(5);
            secondImg.clipsToBounds = YES;
            secondImg.layer.borderColor = FONTCOLOR_LIGHTGRAY.CGColor;
            secondImg.layer.borderWidth = 1;
            if (_model) {
                if (_model.business.count > 1) {
                    [secondImg sd_setImageWithURL:[NSURL URLWithString:_model.business[1][@"qualification_pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
                }
            }
            [cell addSubview:secondImg];
            
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(190), SCREEN_WIDTH, ScreenX375(10))];
            bottomView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            [cell addSubview:bottomView];
            return cell;
        }else {
            static NSString *cellId = @"likeCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *view in cell.subviews) {
                [view removeFromSuperview];
            }
            UIView *headView = [self headViewSetWithName:@"擅长项目"];
            [cell addSubview:headView];
            
            if (_model) {
                for (int i = 0; i < _model.labels.count; i++) {
                    UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13) + i % 3 * ScreenX375(94), ScreenX375(64) + i / 3 * ScreenX375(40), ScreenX375(84), ScreenX375(30))];
                    tagLabel.text = _model.labels[i][@"name"];
                    tagLabel.textAlignment = NSTextAlignmentCenter;
                    tagLabel.font = Kfont(14);
                    tagLabel.layer.cornerRadius = ScreenX375(5);
                    tagLabel.clipsToBounds = YES;
                    tagLabel.layer.borderWidth = 0.8;
                    tagLabel.layer.borderColor = FONTCOLOR_LIGHTGRAY.CGColor;
                    [cell addSubview:tagLabel];
                }
            }
            return cell;
        }
    }else if ([_type isEqualToString:@"1"]) {
        ShopGoodsTableViewCell *cell = [ShopGoodsTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.shopGoodsListModel = self.dataArray[_saleStatu][indexPath.row];
        return cell;
    }else if ([_type isEqualToString:@"2"]){
        if (indexPath.row < _createSpaceData.count) {
            FirstPageCell *cell = [FirstPageCell cellWithTableView:tableView andIndexPath:indexPath];
            CreateSpaceModel *model = [self.createSpaceData objectAtIndex:indexPath.row];
            [cell initwithModel:model];
            if (!_isMine) {
                cell.deleteBtn.hidden = YES;
            }else {
                cell.deleteBtn.hidden = NO;
            }
            cell.deleteClick = ^{
                [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/shop_delete_creative_space",REQUESTHEADER] andParameter:@{@"token":[FPLoginAndRegisterTools getToken],@"goods_id":model.design_id} success:^(id successResponse) {
                    NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                    if ([status integerValue] == 1) {
                        [HttpManager showNoteMsg:@"删除成功"];
                        self.CPcurrentPage = 1;
                        [self getCreateSpaceDate];
                    }
                } andFailure:^(id failureResponse) {
                    [HttpManager showFail];
                }];
            };
            return cell;
        }else {
            static NSString *cellId = @"AddCreateCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                
                UIButton *submit =[[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(28), ScreenX375(349), ScreenX375(44))];
                [submit setTitle:@"+添加商品" forState:UIControlStateNormal];
                [submit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                submit.backgroundColor = ThemeColor;
                submit.layer.cornerRadius = 22.0;
                submit.tag = 555;
                [submit addTarget:self action:@selector(addGoods:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:submit];
            }
            return cell;
        }
    }else{
        if (indexPath.row < _model.pics.count) {
            FirstPageCell *cell = [FirstPageCell cellWithTableView:tableView andIndexPath:indexPath];
            [cell initWithDic:_model.pics[indexPath.row]];
            if (!_isMine) {
                cell.deleteBtn.hidden = YES;
            }else {
                cell.deleteBtn.hidden = NO;
            }
            cell.deleteClick = ^{
                NSDictionary *dic = self.model.pics[indexPath.row];
                [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/shop_delete_index_pic",REQUESTHEADER] andParameter:@{@"token":[FPLoginAndRegisterTools getToken],@"id":dic[@"id"]} success:^(id successResponse) {
                    NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                    if ([status integerValue] == 1) {
                        [HttpManager showNoteMsg:@"删除成功"];
                        [self getData];
                    }
                } andFailure:^(id failureResponse) {
                    [HttpManager showFail];
                }];
            };
            return cell;
        }else {
            static NSString *cellId = @"AddHomeCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                
                UIButton *submit =[[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(28), ScreenX375(349), ScreenX375(44))];
                [submit setTitle:@"+添加商品" forState:UIControlStateNormal];
                [submit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                submit.backgroundColor = ThemeColor;
                submit.layer.cornerRadius = 22.0;
                submit.tag = 666;
                [submit addTarget:self action:@selector(addGoods:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:submit];
            }
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _goodsTableView) {
        if (_selectIndex == indexPath.row) {
            _selectIndex = -1;
        }else {
            _selectIndex = indexPath.row;
        }
        [_goodsTableView reloadData];
    }else {
        if ([_type isEqualToString:@"1"]) {
            ShopGoodsListModel *model = self.dataArray[_saleStatu][indexPath.row];
            ShopGoodsDetailVC *goodsDetailVC = [[ShopGoodsDetailVC alloc] init];
            //将商品ID传入下个界面
            goodsDetailVC.goods_id = model.Id;
            goodsDetailVC.statu = @"0";
            [self.navigationController pushViewController:goodsDetailVC animated:true];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _goodsTableView) {
        return ScreenX375(87);
    }
    if ([_type isEqualToString:@"1"]) {
         return ScreenX375(100);
    }else if ([_type isEqualToString:@"2"]) {
        if (indexPath.row == _createSpaceData.count) {
            return ScreenX375(100);
        }
        CreateSpaceModel *model = [self.createSpaceData objectAtIndex:indexPath.row];
        if (!model.design_name) {
            return ScreenX375(180);
        }
        return ScreenX375(195);
    }else if ([_type isEqualToString:@"0"]) {
        if (indexPath.row == _model.pics.count) {
            return ScreenX375(100);
        }
        NSDictionary *dic = self.model.pics[indexPath.row];
        if ([dic[@"name"] class] == [NSNull class]) {
            return ScreenX375(180);
        }
        return ScreenX375(195);
    }else if ([_type isEqualToString:@"3"]) {
        if (indexPath.row == 0) {
            return ScreenX375(153);
        }else if (indexPath.row == 1) {
            return ScreenX375(322 - 264);
        }else if (indexPath.row == 2) {
            return ScreenX375(140);
        }else if (indexPath.row == 3) {
            return ScreenX375(124);
        }else if (indexPath.row == 4) {
            return ScreenX375(200);
        }
        return ScreenX375(104) + (_model.labels.count > 0 ? _model.labels.count - 1: _model.labels.count) / 3 * ScreenX375(40);
    }
    return ScreenX375(195);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == _goodsTableView) {
        return 0.01;
    }
    return ScreenX375(86);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == _goodsTableView) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(86))];
    view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    
    if (self.operateView == nil) {
        self.operateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(76))];
        self.operateView.backgroundColor = [UIColor whiteColor];
        
        NSArray *nameArr = @[@"店铺首页",@"全部商品",@"创意空间",@"店铺资料"];
        NSArray *normalArr = @[@"店铺首页",@"全部商品",@"创意空间2",@"店铺资料"];
        NSArray *selectArr = @[@"店铺首页2",@"全部商品2",@"创意空间",@"店铺资料2"];
        for (int i = 0; i < nameArr.count; i++) {
            UIButton *typeBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH / 4, 0, SCREEN_WIDTH / 4, ScreenX375(76))];
            [typeBtn setTitle:nameArr[i] forState:UIControlStateNormal];
            [typeBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
            [typeBtn setTitleColor:REXADECIMALCOLOR(@"#999999") forState:UIControlStateNormal];
            [typeBtn setImage:[UIImage imageNamed:normalArr[i]] forState:UIControlStateNormal];
            [typeBtn setImage:[UIImage imageNamed:selectArr[i]] forState:UIControlStateSelected];
            typeBtn.titleLabel.font = Kfont(14);
            //设置文字偏移：向下偏移图片高度＋向左偏移图片宽度 （偏移量是根据［图片］大小来的，这点是关键）
            typeBtn.titleEdgeInsets = UIEdgeInsetsMake(HEIGHT(typeBtn.imageView) + 10, -WIDTH(typeBtn.imageView), 0, 0);
            //设置图片偏移：向上偏移文字高度＋向右偏移文字宽度 （偏移量是根据［文字］大小来的，这点是关键）
            typeBtn.imageEdgeInsets = UIEdgeInsetsMake(-HEIGHT(typeBtn.titleLabel), 0, 0, -WIDTH(typeBtn.titleLabel));
            typeBtn.tag = 100 + i;
            if (i == 0) {
                _selectBtn = typeBtn;
                typeBtn.selected = YES;
            }
            [typeBtn addTarget:self action:@selector(typeClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.operateView addSubview:typeBtn];
        }
        
    }
    [view addSubview:self.operateView];
    
    return view;
}

- (void)typeClick:(UIButton *)button {
    if (button.tag != 103) {
        _selectBtn.selected = NO;
        button.selected = YES;
        _selectBtn = button;
        _type = [NSMutableString stringWithFormat:@"%ld",button.tag - 100];
        NSLog(@"%@",_type);
        [self.tableView reloadData];
    }else {
        if (_isMine) {
            ShopSetViewController *vc = [[ShopSetViewController alloc] init];
            vc.shopInfo = _model;
            vc.shopInfoId = _model.Id;
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            _selectBtn.selected = NO;
            button.selected = YES;
            _selectBtn = button;
            _type = [NSMutableString stringWithFormat:@"3"];
            [self.tableView reloadData];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _tableView && scrollView.contentOffset.y > 0) {
        if (scrollView.contentOffset.y > ScreenX375(192) - 21 * SCREEN_WIDTH / 375) {
            self.navigationController.navigationBarHidden = NO;
            if (_isMine) {
                _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT);
            }else {
                _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49));
                _buttonView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(49) - NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(49));
            }
        }else {
            self.navigationController.navigationBarHidden = YES;
            if (_isMine) {
                _tableView.frame = CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT);
            }else {
                _tableView.frame = CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT - ScreenX375(49));
                _buttonView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(49), SCREEN_WIDTH, ScreenX375(49));
            }
        }
    }
}

-(void)getCreateSpaceDate{
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"token":kNSUDefaultReadKey(@"token"),@"page":[NSNumber numberWithInteger:_CPcurrentPage],@"shop_id":_shop_id}];
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/shop_creative_space",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error = nil;
            if (self.CPcurrentPage == 1) {
                [self.createSpaceData removeAllObjects];
            }
            self.CPtotalPage = [successResponse[@"data"][@"last_page"] integerValue];
            if (self.CPtotalPage == self.CPcurrentPage) {
                self.tableView.mj_footer.hidden = YES;
            }
            for (NSDictionary *dict in successResponse[@"data"][@"data"]) {
                CreateSpaceModel *model = [[CreateSpaceModel alloc] initWithDictionary:dict error:&error];
                model.design_id = dict[@"id"];
                [self.createSpaceData addObject:model];
            }
            if ([successResponse[@"data"][@"data"] count] == 0) {
                [HttpManager showNoteMsg:@"暂无数据"];
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    }];
}

- (void)getGoodsData {
    _saleStatu = 1;
    NSString *url = [NSString stringWithFormat:@"%@app/shop_product",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":_shop_id,@"is_sale":@"1",@"page":self.currentPageArray[_saleStatu]};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSLog(@"%@",successResponse);
            if ([self.currentPageArray[self.saleStatu] integerValue] == 1) {
                [self.dataArray[self.saleStatu] removeAllObjects];
            }
            [self.pageArray setObject:[NSString stringWithFormat:@"%@",successResponse[@"data"][@"last_page"]] atIndexedSubscript:self.saleStatu];
            NSError *error = nil;
            for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                ShopGoodsListModel *model = [[ShopGoodsListModel alloc] initWithDictionary:dic error:&error];
                model.statu = [NSString stringWithFormat:@"%ld",(long)self.saleStatu];
                [self.dataArray[self.saleStatu] addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
            self.tableView.mj_header.hidden = YES;
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    }];
}

- (void)hiddenAddView {
    self.selectIndex = -1;
    [self.goodsTableView reloadData];
    self.addImage = nil;
    [self.addPhotoBtn setBackgroundImage:[UIImage imageNamed:@"虚线边框"] forState:UIControlStateNormal];
    [self.addPhotoBtn setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
    [self.addPhotoBtn setTitle:@"发布设计图片（仅供展示）" forState:UIControlStateNormal];
    [self.addGoodsView removeFromSuperview];
}

- (void)addGoods:(UIButton *)button {
    if (button.tag == 555) {
        _isHomeSet = NO;
    }else {
        _isHomeSet = YES;
    }
    
    if (!self.addGoodsView) {
        self.addGoodsView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.addGoodsView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self.addGoodsView addTarget:self action:@selector(hiddenAddView) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *contentBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(13), (SCREEN_HEIGHT - ScreenX375(651 - 87)) / 2, SCREEN_WIDTH - ScreenX375(26), ScreenX375(651 - 87))];
        contentBg.backgroundColor = [UIColor whiteColor];
        contentBg.layer.cornerRadius = ScreenX375(5);
        [self.addGoodsView addSubview:contentBg];
        
        UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(62), ScreenX375(10), ScreenX375(26), ScreenX375(26))];
        [closeBtn setImage:ImageWithName(@"×") forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(hiddenAddView) forControlEvents:UIControlEventTouchUpInside];
        [contentBg addSubview:closeBtn];
        
        self.goodsTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, ScreenX375(46), SCREEN_WIDTH - ScreenX375(26), ScreenX375(87 * 3))];
        self.goodsTableView.delegate = self;
        self.goodsTableView.dataSource = self;
        self.goodsTableView.rowHeight = ScreenX375(72);
        self.goodsTableView.backgroundColor = [UIColor whiteColor];
        [contentBg addSubview:self.goodsTableView];
        
        self.goodsTableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(goodsHeaderRefresh)];
        self.goodsTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(goodsFooterRefresh)];
        [self.goodsTableView.mj_header beginRefreshing];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(87 * 3) + ScreenX375(46), SCREEN_WIDTH - ScreenX375(26), ScreenX375(10))];
        line.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
        [contentBg addSubview:line];
        
        _addPhotoBtn = [FPCustomButton buttonWithType:UIButtonTypeCustom];
        _addPhotoBtn.frame = CGRectMake(ScreenX375(15), CGRectGetMaxY(line.frame) + ScreenX375(15), SCREEN_WIDTH - ScreenX375(56), ScreenX375(110));
        [_addPhotoBtn setBackgroundImage:[UIImage imageNamed:@"虚线边框"] forState:UIControlStateNormal];
        [_addPhotoBtn setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
        [_addPhotoBtn setTitle:@"发布设计图片（仅供展示）" forState:UIControlStateNormal];
        [_addPhotoBtn setTitleColor:RGB_COLOR(210,210,210) forState:UIControlStateNormal];
        _addPhotoBtn.titleLabel.font = Kfont(14);
        [_addPhotoBtn addTarget:self action:@selector(addPhotoClick) forControlEvents:UIControlEventTouchUpInside];
        [contentBg addSubview:_addPhotoBtn];
        
        UIButton *submit =[[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(15), CGRectGetMaxY(_addPhotoBtn.frame) + ScreenX375(30), SCREEN_WIDTH - ScreenX375(56), ScreenX375(44))];
        [submit setTitle:@"发 布" forState:UIControlStateNormal];
        [submit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        submit.backgroundColor = ThemeColor;
        submit.layer.cornerRadius = 22.0;
        [submit addTarget:self action:@selector(publish) forControlEvents:UIControlEventTouchUpInside];
        [contentBg addSubview:submit];
    }
    [self.view addSubview:self.addGoodsView];
}

- (void)goodsHeaderRefresh {
    self.currentPage = 1;
//    [self getUpData];
    [self getGoodsData];
}

- (void)goodsFooterRefresh {
    if (self.currentPage < self.page) {
        
        self.currentPage++;
        [self getUpData];
        [self.tableView.mj_footer endRefreshing];
    }else{
        [HttpManager showNoteMsg:@"已经到底啦"];
        [self.goodsTableView.mj_footer endRefreshing];
        self.goodsTableView.mj_footer.hidden = YES;
    }
}

- (void)getUpData {
    NSString *url = [NSString stringWithFormat:@"%@app/shop_product",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":_model.Id,@"is_sale":@"1",@"page":[NSNumber numberWithInteger:self.currentPage]};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            if (self.currentPage == 1) {
                [self.addGoodsArray removeAllObjects];
            }
            self.page = [successResponse[@"data"][@"last_page"] integerValue];
            NSError *error = nil;
            for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                ShopGoodsListModel *model = [[ShopGoodsListModel alloc] initWithDictionary:dic error:&error];
                model.statu = @"1";
                [self.addGoodsArray addObject:model];
            }
            [self.goodsTableView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.goodsTableView.mj_header isRefreshing]) {
            [self.goodsTableView.mj_header endRefreshing];
        }else if ([self.goodsTableView.mj_footer isRefreshing]) {
            [self.goodsTableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        if ([self.goodsTableView.mj_header isRefreshing]) {
            [self.goodsTableView.mj_header endRefreshing];
        }else if ([self.goodsTableView.mj_footer isRefreshing]) {
            [self.goodsTableView.mj_footer endRefreshing];
        }
    }];
}

- (void)publish {
    if (!_addImage && _selectIndex == -1) {
        [HttpManager showNoteMsg:@"请选择要发布的产品"];
        return;
    }

    NSString *url = [NSString stringWithFormat:@"%@app/shop_add_index_pic",REQUESTHEADER];
    if (!_isHomeSet) {
        url = [NSString stringWithFormat:@"%@app/shop_add_creative_space",REQUESTHEADER];
    }
    if (_selectIndex != -1) {
        ShopGoodsListModel *model = _addGoodsArray[_selectIndex];
        NSDictionary *parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":_shop_id,@"pic":model.pic,@"name":model.design_name,@"price":model.price};
        if (!_isHomeSet) {
            parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":_shop_id,@"pic":model.pic,@"design_name":model.design_name,@"price":model.price};
        }
        
        [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
            NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
            if ([status integerValue] == 1) {
                [HttpManager showNoteMsg:@"发布成功"];
                if (self.isHomeSet) {
                    [self getData];
                }else {
                    self.CPcurrentPage = 1;
                    [self getCreateSpaceDate];
                }
                [self hiddenAddView];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }else {
        [HttpManager uploadImageArr:[NSMutableArray arrayWithObject:_addImage] success:^(id successResponse) {
            NSString *pic = successResponse[0];
            NSDictionary *parameter = @{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":self.shop_id,@"pic":pic};
            
            [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
                NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                if ([status integerValue] == 1) {
                    [HttpManager showNoteMsg:@"发布成功"];
                    if (self.isHomeSet) {
                        [self getData];
                    }else {
                        self.CPcurrentPage = 1;
                        [self getCreateSpaceDate];
                    }
                    
                    [self hiddenAddView];
                }
            } andFailure:^(id failureResponse) {
                [HttpManager showFail];
            }];
        }];
    }
}

- (void)addPhotoClick {
    if (!self.pickerController) {
        self.pickerController = [[UIImagePickerController alloc] init];
        self.pickerController.delegate = self;
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *send = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }];
    UIAlertAction *get = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.pickerController.allowsEditing = YES;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }];
    
    [alert addAction:cancel];
    [alert addAction:send];
    [alert addAction:get];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.selectIndex = -1;
    [self.goodsTableView reloadData];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [_addPhotoBtn setBackgroundImage:image forState:UIControlStateNormal];
    [_addPhotoBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_addPhotoBtn setTitle:@"" forState:UIControlStateNormal];
    _addImage = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self.pickerController dismissViewControllerAnimated:YES completion:nil];
}

- (UIView *)headViewSetWithName:(NSString *)name {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(54))];
    headView.backgroundColor = [UIColor whiteColor];
    
    UIView *note_line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
    note_line.backgroundColor = ThemeColor;
    [headView addSubview:note_line];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(150), ScreenX375(24))];
    noteLabel.text = name;
    noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
    noteLabel.font = Kfont(18);
    [headView addSubview:noteLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(53.5), SCREEN_WIDTH, 0.5)];
    line.backgroundColor = LineColor;
    [headView addSubview:line];
    
    return headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
